@extends('backend.layouts.base')
@section('title', '存储设置')
@section('content')
    <div class="all">
        <div id="app" v-cloak>
            <el-form ref="form" :model="form" :rules="rules" label-width="15%">
                <div class="vue-head head-tab-con">
                    <el-radio-group v-model="display_type">
                        <el-radio-button label="1" @click.native="gotoOther(1)">全局设置</el-radio-button>
                        <el-radio-button label="2" @click.native="gotoOther(2)">远程存储设置</el-radio-button>
                    </el-radio-group>
                </div>
                <div class="vue-head">
                    <div class="vue-main-title">
                        <div class="vue-main-title-left"></div>
                        <div class="vue-main-title-content">远程存储设置</div>
                    </div>
                    <div class="vue-main-form">
                        <el-form-item label="类型" prop="type">
                            <el-radio v-model="form.type" :label="0">关闭</el-radio>
                            <el-radio v-model="form.type" :label="1"
                            >阿里云OSS</el-radio
                            >
                            <el-radio v-model="form.type" :label="2"
                            >腾讯云存储</el-radio
                            >
                            {{--<div class="tip" v-if="form.type!=0">
                                把/static/upload/images/整个images目录上传到对象存储上
                            </div>--}}
                            <div class="tip" v-if="form.type==1">
                                阿里云oss上传教程,请前往阿里云了解
                            </div>
                            <div class="tip" v-if="form.type==2">
                                腾讯云对象存储上传教程,请前往腾讯云了解
                            </div>
                        </el-form-item>
                    </div>
                    <div v-if="form.type == 1">
                        <div class="vue-main-form">
                            <el-form-item label="Access Key ID" prop="key">
                                <el-input v-model="form.alioss.key"></el-input>
                                <div class="tip">Access Key ID是您访问阿里云API的密钥，具有该用户完全的权限。请您妥善保管。</div>
                            </el-form-item>
                        </div>
                        <div class="vue-main-form">
                            <el-form-item label="Access Key Secret" prop="secret">
                                <el-input v-model="form.alioss.secret"></el-input>
                                <div class="tip">Access Key Secret是您访问阿里云API的密钥，具有该用户完全的权限。请您妥善保管。（填写完Access Key ID和Access Key Secret后请选择bucket）</div>
                            </el-form-item>
                        </div>
                        <div class="vue-main-form">
                            <el-form-item label="Bucket外网访问域名" prop="url">
                                <el-input v-model="form.alioss.url"></el-input>
                            </el-form-item>
                        </div>
                        {{--<div class="vue-main-form">
                            <el-form-item label="endpoint" prop="endpoint">
                                <el-input v-model="form.alioss.endpoint"></el-input>
                            </el-form-item>
                        </div>
                        <div class="vue-main-form">
                            <el-form-item label="bucket" prop="bucket">
                                <el-input v-model="form.alioss.bucket"></el-input>
                            </el-form-item>
                        </div>--}}
                    </div>
                    <div v-if="form.type == 2">
                        <div class="vue-main-form">
                            <el-form-item label="APPID" prop="appid">
                                <el-input v-model="form.cos.appid"></el-input>
                                <div class="tip">APPID 是您项目的唯一ID</div>
                            </el-form-item>
                        </div>
                        <div class="vue-main-form">
                            <el-form-item label="SecretID" prop="secretid">
                                <el-input v-model="form.cos.secretid"></el-input>
                                <div class="tip">SecretID 是您项目的安全秘钥，具有该账户完全的权限，请妥善保管</div>
                            </el-form-item>
                        </div>
                        <div class="vue-main-form">
                            <el-form-item label="SecretKEY" prop="secretkey">
                                <el-input v-model="form.cos.secretkey"></el-input>
                                <div class="tip">SecretKEY 是您项目的安全秘钥，具有该账户完全的权限，请妥善保管</div>
                            </el-form-item>
                        </div>
                        <div class="vue-main-form">
                            <el-form-item label="访问域名" prop="url">
                                <el-input v-model="form.cos.url"></el-input>
                            </el-form-item>
                        </div>
                        <div class="vue-main-form">
                            <el-form-item label="Bucket" prop="bucket">
                                <el-input v-model="form.cos.bucket"></el-input>
                                <div class="tip">请保证bucket为可公共读取的</div>
                            </el-form-item>
                        </div>
                        <div class="vue-main-form">
                            <el-form-item label="bucket所在区域" prop="region">
                                <el-select
                                        v-model="form.cos.region"
                                        style="width:70%;"
                                        placeholder="请选择bucket所在区域"
                                        clearable
                                >
                                    <el-option
                                            v-for="item in cos_regions"
                                            :key="item.key"
                                            :label="item.value"
                                            :value="item.key"
                                    >
                                    </el-option>
                                </el-select>
                                <div class="tip">选择bucket对应的区域，如果没有选择无</div>
                            </el-form-item>
                        </div>
                    </div>
                </div>
            </el-form>
            <div class="vue-page">
                <div class="vue-center">
                    <el-button type="primary" @click="submitForm('form')">提交</el-button>
                    <el-button @click="goBack">返回</el-button>
                </div>
            </div>
        </div>
    </div>

    <script>
        var app = new Vue({
            el:"#app",
            delimiters: ['[[', ']]'],
            name: 'settingRemoteAttach',
            data() {
                return{
                    cos_regions: [],
                    display_type:'2',
                    form:{
                        type: 0,
                        alioss: {
                            key: "",
                            secret: "",
                            endpoint: "",
                            bucket: "",
                            url: ""
                        },
                        cos: {
                            appid: "",
                            secretid: "",
                            secretkey: "",
                            bucket: "",
                            region: "",
                            url: ""
                        }
                    },
                    rules:{
                    },
                }
            },
            created() {
            },
            mounted() {
                this.getData();
            },
            methods: {
                getData() {
                    let loading = this.$loading({target:document.querySelector(".content"),background: 'rgba(0, 0, 0, 0)'});
                    this.$http.post('{!! absolute_admin_url('sys.setting.remoteAttachInfo') !!}',{}).then(function (response) {
                            if (response.data.result){
                                if(response.data.data.setting){
                                    this.form.type = response.data.data.setting.type;
                                    this.form.alioss = response.data.data.setting.alioss;
                                    this.form.cos = response.data.data.setting.cos;
                                }
                                if(response.data.data.cos_regions){
                                    this.cos_regions = response.data.data.cos_regions;
                                }
                            }
                            else {
                                this.$message({message: response.data.msg,type: 'error'});
                            }
                            loading.close();
                        },function (response) {
                            this.$message({message: response.data.msg,type: 'error'});
                            loading.close();
                        }
                    );
                },
                submitForm(formName) {
                    console.log(this.form)
                    let that = this;
                    this.$refs[formName].validate((valid) => {
                        if (valid) {
                            let loading = this.$loading({target:document.querySelector(".content"),background: 'rgba(0, 0, 0, 0)'});
                            this.$http.post('{!! absolute_admin_url('sys.setting.remoteAttachSave') !!}',{setting:this.form}).then(response => {
                                if (response.data.result) {
                                    this.$message({type: 'success',message: '操作成功!'});
                                } else {
                                    this.$message({message: response.data.msg,type: 'error'});
                                }
                                loading.close();
                            },response => {
                                this.$message({message: response.data.msg,type: 'error'});
                                loading.close();
                            });
                        }
                        else {
                            console.log('error submit!!');
                            return false;
                        }
                    });
                },

                goBack() {
                    history.go(-1)
                },
                gotoOther(type) {
                    let url = ""
                    if(type==1) {
                        url = '{!! absolute_admin_url('sys.setting.attach') !!}'
                    }else if(type==2) {
                        // url = ''
                    }
                    if(url) {
                        window.location.href=url
                    }
                },
            },
        })

    </script>
@endsection
